<?php

namespace Modules\Production\Repository;

use Illuminate\Support\Facades\DB;
use Modules\Production\Models\ProductionProcess;
use Modules\Production\Models\ProductionProcessDetail;

/**
 * 生产工单工序
 * Class ProcessRepository
 * @package Modules\Production\Repository
 */
class ProcessRepository
{
    /**
     * 分页列表
     * @param $request
     * @return mixed
     */
    static public function getList($request){

        $builder = self::getWhere($request);

        $list = $builder->with(['detail'])->paginate()->toArray();

        return $list;

    }

    /**
     * 查询条件拼接
     * @param $request
     * @return ProductionProcess
     */
    static public function getWhere($request){

        $builder = new ProductionProcess();

        $canSearchFields = $builder->getFields();

        foreach ($request as $key => $val){

            if(!in_array($key,$canSearchFields) || $val == ''){
                continue;
            }

            $builder = $builder->where($key,$val);

        }

        return $builder;

    }

    /**
     * 检查工单是否已经工序报工
     * @param $productionId
     * @return bool
     */
    static public function hasProcessReport($productionId){
        $reportQty = ProductionProcess::where('production_order_id',$productionId)->sum('make_qty');
        return $reportQty > 0 ? true : false;
    }

}
